//
//  BRFAQListTableViewCell.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/10/8.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRFAQListTableViewCell.h"


@interface BRFAQListTableViewCell ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation BRFAQListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.typeText.font = [UIFont br_fontB:17];
    self.typeText.textColor = UIColor.qd_titleTextColor;
    self.lineV.backgroundColor = UIColor.qd_lineColor2;
    self.contentBGView.backgroundColor = UIColor.qd_backgroundColor2;
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.tableView.backgroundColor = UIColorClear;
    self.contentView.backgroundColor = UIColor.qd_backgroundColorLighten;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([BRFAQSubTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"BRFAQSubTableViewCell"];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
}

- (void)setDatas:(NSArray *)datas {
    _datas = datas;

    [self.tableView  reloadData];

//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        
//    });
//    
//    if (self.cellHeight) {
//        self.cellHeight(self.tableView.contentSize.height);
//    }
//    NSLog(@"%.2f",self.tableView.contentSize.height);
}

- (void)setType:(NSInteger)type {
    _type = type;
    NSString *imgTypeStr = [[BRThemeManage themeName] isEqualToString:QDThemeIdentifierDark] ? @"faq_3type" : @"faq_type";
    NSString *imgStr = [NSString stringWithFormat:@"%@%ld",imgTypeStr,type];
    self.typeImg.image = UIImageMake(imgStr);
    
    self.typeText.text = [BRTools strOfType:type];
    
    [self colorOfType:type];
}

- (void)colorOfType:(NSInteger)type {
    
    NSArray *colors = @[UIColor.qd_tintColor24,
                        UIColor.qd_tintColor25,
                        UIColor.qd_tintColor10,
                        UIColor.qd_tintColor11,
                        UIColor.qd_tintColor12,
                        UIColor.qd_tintColor13,
                        UIColor.qd_tintColor14,
                        UIColor.qd_tintColor15,
                        UIColor.qd_tintColor16];
    NSInteger index = type - 1;
    if (index > 0 || index < colors.count) {
        
        self.typeView.backgroundColor = colors[index];
        return;
    }
    self.typeView.backgroundColor = UIColorBRTheme1;
}

#pragma mark - delegate --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datas.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    BRFAQSubTableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:@"BRFAQSubTableViewCell"];
    
    BRFAQModel *model = self.datas[indexPath.row];
    cell.dto = model;
    if (indexPath.row == self.datas.count - 1) {
        cell.lineV.hidden = YES;
    }else {
        cell.lineV.hidden = NO;
    }
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat cel_h  = 0;
    BRFAQModel *model = self.datas[indexPath.row];
    if (model.cell_h != 0) {
        return model.cell_h;
    }
    
    NSString *string = model.title;
    cel_h = [string jk_heightWithFont:[UIFont br_fontM:14] constrainedToWidth:kScreenWidth - 110.] + 27.;
    model.cell_h = cel_h;
    return  cel_h;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"%.2f",tableView.contentSize.height);
    BRFAQModel *model = self.datas[indexPath.row];
    if (self.faqClick) {
        self.faqClick(model);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
